package top.wangjiu.reactive.rpc.autoconfigure.client;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import top.wangjiu.reactive.rpc.config.ClientConfiguration;
import top.wangjiu.reactive.rpc.registration.LocalConfigServerAddrList;
import top.wangjiu.reactive.rpc.registration.ServerAddrList;
import top.wangjiu.reactive.rpc.spring.client.RpcClientAnnotationBeanPostProcessor;

@Configuration
@EnableConfigurationProperties({ReactiveRpcClientProperties.class})
@ConditionalOnClass(RpcClientAnnotationBeanPostProcessor.class)
@ConditionalOnBean(ServerAddrList.class)
@ConditionalOnProperty(prefix = ReactiveRpcClientProperties.CONFIG_PREFIX,value = "enable",havingValue = "true")
@Import(RpcClientAnnotationBeanPostProcessor.class)
public class ReactiveRpcClientAutoConfiguration implements ApplicationContextAware, InitializingBean {

    private ApplicationContext applicationContext;

//    @Bean
//    @ConditionalOnMissingBean
//    public ServerAddrList serverAddrList(){
//        return new LocalConfigServerAddrList();
//    }

    @Bean
    @ConditionalOnMissingBean
    public ClientConfiguration clientConfiguration(ReactiveRpcClientProperties reactiveRpcClientProperties,ServerAddrList serverAddrList){
        ClientConfiguration configuration = new ClientConfiguration();
        configuration.setServerAddrList(serverAddrList);
        return configuration;
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
